﻿/*-----------------------------------------------
// File: BattleEndCalc.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle
{
    public enum BattleResultCondition {
        Enemy_All_Dead,
        OurSide_All_Dead,
        Round_Max
    }
    public class BattleEndCalc
    {
        Engine.EngineData engineData;
        //BattleType currentBattleType;
        BattleCamp currentSubjectCamp;

        // 胜利失败条件， 或的关系
        List<BattleResultCondition> winConditions = new List<BattleResultCondition>();
        List<BattleResultCondition> loseConditions = new List<BattleResultCondition>();

        public void Init(BattleType battleType, BattleCamp subjectCamp, Engine.EngineData data) {
            engineData = data;
            InitBattleResultConditions(battleType, subjectCamp);
        }

        void InitBattleResultConditions(BattleType type, BattleCamp subjectCamp) {
            //currentBattleType = type;
            currentSubjectCamp = subjectCamp;
            winConditions.Clear();
            loseConditions.Clear();

            if (type == BattleType.PVE
                || type == BattleType.WANTED
                || type == BattleType.PVP
                || type == BattleType.FIELD_PVP
                || type == BattleType.FIELD_PVE
                || type == BattleType.FIELD_BOSS
                || type == BattleType.RECRUIT_SHOW)
            {
                if (subjectCamp == BattleCamp.Attacker)
                {
                    /*
                        Attacker
                        胜利条件：敌方全部阵亡
                        失败条件：我方全部阵亡 or 回合数到达上限
                    */
                    winConditions.Add(BattleResultCondition.Enemy_All_Dead);
                    loseConditions.Add(BattleResultCondition.OurSide_All_Dead);
                    loseConditions.Add(BattleResultCondition.Round_Max);
                }
                else {
                    /*
                        Defender
                        胜利条件：敌方全部阵亡 or 回合数到达上限
                        失败条件：我方全部阵亡
                    */
                    winConditions.Add(BattleResultCondition.Enemy_All_Dead);
                    winConditions.Add(BattleResultCondition.Round_Max);
                    loseConditions.Add(BattleResultCondition.OurSide_All_Dead);
                }
            }
            else {
                // 默认条件
                if (subjectCamp == BattleCamp.Attacker)
                {
                    /*
                        Attacker
                        胜利条件：敌方全部阵亡
                        失败条件：我方全部阵亡 or 回合数到达上限
                    */
                    winConditions.Add(BattleResultCondition.Enemy_All_Dead);
                    loseConditions.Add(BattleResultCondition.OurSide_All_Dead);
                    loseConditions.Add(BattleResultCondition.Round_Max);
                }
                else {
                    /*
                        Defender
                        胜利条件：敌方全部阵亡 or 回合数到达上限
                        失败条件：我方全部阵亡
                    */
                    winConditions.Add(BattleResultCondition.Enemy_All_Dead);
                    winConditions.Add(BattleResultCondition.Round_Max);
                    loseConditions.Add(BattleResultCondition.OurSide_All_Dead);
                }
            }
        }

        /// <summary>
        /// 检查战斗结果
        /// </summary>
        /// <returns>是否胜利</returns>
        public bool CheckResult() {
            if (IsMatchConditions(currentSubjectCamp, winConditions)) {
                return true;
            }
            if (IsMatchConditions(currentSubjectCamp, loseConditions)) {
                return false;
            }
            Debug.LogError("[BattleEndCalc] CheckResult Error. No win or lose conditons match!");
            return false;
        }


        BattleCamp GetOppoCamp(BattleCamp camp) {
            if (camp == BattleCamp.Attacker)
                return BattleCamp.Defender;
            else
                return BattleCamp.Attacker;
        }
        bool IsMatchConditions(BattleCamp camp, List<BattleResultCondition> conditions) {
            foreach (var condition in conditions) {
                switch (condition) {
                    case BattleResultCondition.Enemy_All_Dead:
                        if (IsSideAllDead(GetOppoCamp(camp))) {
                            return true;
                        }
                        break;
                    case BattleResultCondition.OurSide_All_Dead:
                        if (IsSideAllDead(camp)) {
                            return true;
                        }
                        break;
                    case BattleResultCondition.Round_Max:
                        if (IsRoundMax()) {
                            return true;
                        }
                        break;
                }
            }
            return false;
        }



        bool IsRoundMax() {
            if (engineData.roundNum > engineData.roundMax) {
                return true;
            }
            return false;
        }

        bool IsSideAllDead(BattleCamp camp) {
            List<Engine.LogicModel.FActor> actors;
            if (camp == BattleCamp.Attacker)
            {
                actors = engineData.attacker.actors;
            }
            else
            {
                actors = engineData.defender.actors;
            }
            foreach (var actor in actors)
            {
                if (actor.state != Engine.LogicModel.FActorState.DEAD && actor.state != Engine.LogicModel.FActorState.WAEKDEAD)
                {
                    return false;
                }
            }
            return true;
        }






    }
}
